package net.huashitong.supervision.task.service;

import com.sirdc.modules.core.dao.BaseDao;
import com.sirdc.modules.sys.base.StringPKService;
import com.sirdc.modules.sys.service.SysTableService;
import net.huashitong.supervision.task.dao.SpecialTaskDao;
import net.huashitong.supervision.task.dataBean.TaskMajorBean;
import net.huashitong.supervision.task.entity.SpecialTask;
import net.huashitong.supervision.task.filter.SpecialTaskFilter;
import net.huashitong.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @author ling
 * @date: 2020/2/25
 * @time: 10:12
 * @version: 1.0
 * @Description:
 */
@Service
public class SpecialTaskService extends StringPKService<SpecialTask> {
    @Autowired
    private SpecialTaskDao dao;
    @Autowired
    private SysTableService tableService;

    @Override
    protected BaseDao<SpecialTask, String> getDao() {
        return dao;
    }

    public void save(SpecialTask obj,String userId) {
        beforeSave(obj);
        obj.setCreateUser(userId);
        obj.setCreateDate(DateUtil.getCurrentDate());
        obj.setCreateTime(DateUtil.getCurrentTime());

        dao.save(obj);
    }

    @Override
    protected void beforeSave(SpecialTask obj) {
        obj.setSysId(tableService.updateMaxSysId("t_special_task", null));
    }

    public List<SpecialTask> queryFilter(SpecialTaskFilter filter) {

        return dao.queryFilter(filter);
    }


    /**
     * 获取某个主任务下的 所有 重大立项轮次督办轮次
     * @param filter
     * @return
     */
    public List<TaskMajorBean> getMajorList(SpecialTaskFilter filter)
    {
        List<SpecialTask> specialTasks = dao.queryFilter(filter);

        List<TaskMajorBean> listBean = new ArrayList<>();
        for (SpecialTask item : specialTasks) {

            TaskMajorBean bean  = new TaskMajorBean();
            bean.setEvaluation(item.getEvaluations());
            bean.setTaskMainId(item.getTaskMainId());
            bean.setSpecialId(item.getSysId());
            bean.setRoundLimit(item.getRoundLimit());
            bean.setTimeRound(item.getTimeRound());
            bean.setTaskStatus(item.getType());
            bean.setTaskType(item.getTaskType());

            if ("1".equals(item.getTaskType()) ){
                bean.setDeptId(item.getInspectionId());
            }else if("2".equals(item.getTaskType()))
            {
                bean.setDeptId(item.getOverdueDeptId());
            }


            if("2".equals(item.getType()))
            {
                bean.setIsFeedback(1+"");
            }

            listBean.add(bean);

        }

        return listBean;
    }

}
